using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;

namespace IGGCore
{
    public class LineupParser : BaseParser
    {
        /// <summary>
        /// Parse a game lineup
        /// </summary>
        public void ParseLineup(DateTime currentDate, Game game)
        {
            string id = game.Id.Replace("/", "_");
            id = id.Replace("-", "_");
            string url = CoreConstants.MLB_ONLINE_BASE_URL
                + "/" + ToolBox.GetDateURL(currentDate)
                + "/gid_" + id + "/" + CoreConstants.PLAYS_URL;
            XmlDocument xmldoc = new XmlDocument();
            try
            {
                xmldoc.Load(url);
                XmlNode gameNode = xmldoc.SelectSingleNode("game");
                XmlNode lineupsNode = gameNode.SelectSingleNode("lineup");
                XmlNode fieldNode = gameNode.SelectSingleNode("field");
                XmlNode defenseNode = fieldNode.SelectSingleNode("defense");
                XmlNodeList teamNodeList = lineupsNode.SelectNodes("team");

                Team team = game.HomeTeam;
                string teamType = GetAttribute(defenseNode, "type", "UNK");
                if (teamType.Equals("away"))
                {
                    team = game.AwayTeam;
                }
                XmlNodeList manList = defenseNode.SelectNodes("man");
                int idx = 0;
                foreach (XmlNode manNode in manList)
                {
                    string pid = GetAttribute(manNode, "pid", "unknown id");
                    team.Defense[idx] = team.Players[pid];
                    idx++;
                }

                foreach (XmlNode teamNode in teamNodeList)
                {
                    team = game.HomeTeam;
                    teamType = GetAttribute(teamNode, "type", "UNK");
                    if (teamType.Equals("away"))
                    {
                        team = game.AwayTeam;
                    }
                    string pids = GetAttribute(teamNode, "pitcher", "0");
                    if (team.Players.ContainsKey(pids))
                    {
                        team.Lineup[9] = team.Players[pids];
                    }
                    else
                    {
                        team.Lineup[9] = null;
                    }

                    manList = teamNode.SelectNodes("man");
                    idx = 0;
                    foreach (XmlNode manNode in manList)
                    {
                        string pid = GetAttribute(manNode, "pid", "unknown id");
                        team.Lineup[idx] = team.Players[pid];
                        idx++;
                    }
                }
            }
            catch (WebException)
            {
                xmldoc = null;
            }
            finally
            {
                xmldoc = null;
            }
        }

    }
}
